﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SistemaAcademicoBLL.Util;
using SistemaAcademicoBO.Entities;
using SistemaAcademicoBLL;

namespace SistemaAcademico.Pages
{
    public partial class NewProfessor : System.Web.UI.Page
    {
        #region FIELDS
        ApplicationFacade facade = new ApplicationFacade();
        Professor professor;
        int professorId;
        #endregion

        #region PAGE EVENTS
        protected void Page_Load(object sender, EventArgs e)
        {
            #region QueryString
            String action = Request.QueryString["act"];
            if (action == "edit")
            {
                professorId = Convert.ToInt32(Request.QueryString["id"]);
                professor = facade.GetProfessor(professorId);
                txtNome.Text = professor.Nome;
                txtCPF.Text = professor.Cpf;
                txtRG.Text = professor.Rg;
            }
            else
            {
                professorId = 0;
            }
            #endregion

            #region LoadProfessorSubject
            if (!IsPostBack)
            {
                LoadProfessorSubject();
            }
            #endregion
        }

        
        #endregion

        #region PAGE CONTROLS
        protected void btnCadastrar_Click(object sender, EventArgs e)
        {

            try
            {
                if (Validacao())
                {
                    professor = new Professor(professorId, txtNome.Text, Validator.RetiraPontoUniversal(txtRG.Text), Validator.RetiraPontoUniversal(txtCPF.Text), "0");
                    facade.SetProfessor(professor);

                    if (professorId != 0)
                    {
                        //REMOVE TODOS OS professorsSubjects
                        List<ProfessorSubject> allProfessorsSubject = facade.GetAllProfessorsSubjectsByProfessorId(professorId);
                        foreach (ProfessorSubject professorSubject in allProfessorsSubject)
                        {
                            facade.RemoveProfessorSubject(professorSubject.ProfessorId, professorSubject.SubjectId);
                        }
                    }

                    int identCurrent = facade.GetIdentCurrent("Professors");

                    Professor professorIdent = facade.GetProfessor(identCurrent);
                    if (professorIdent.Cpf == professor.Cpf)
                    {
                        foreach (ListItem listItem in lstAddedSubjects.Items)
                        {
                            facade.SetProfessorSubject(new ProfessorSubject(professorIdent.ProfessorId, Convert.ToInt32(listItem.Value)));
                        }
                    }

                    Alert.Show("Cadastro realizado com sucesso!");
                }
            }
            catch 
            {
                Alert.Show("Houve um erro no cadastro.");
            }

        }

        protected void lbtnAdd_Click(object sender, EventArgs e)
        {
            UsefullStuffs.TransferListItem(ref lstSubjects, ref lstAddedSubjects);
        }

        protected void lbtnRemoveSubject_Click(object sender, EventArgs e)
        {
            UsefullStuffs.TransferListItem(ref lstAddedSubjects, ref lstSubjects);
        }
        #endregion

        #region METHODS
        private bool Validacao()
        {
            if (String.IsNullOrEmpty(txtNome.Text))
            {
                Alert.Show("Digite o nome do professor.");
                return false;
            }

            if (String.IsNullOrEmpty(txtRG.Text))
            {
                Alert.Show("Digite o RG do professor.");
                return false;
            }

            if (String.IsNullOrEmpty(txtCPF.Text))
            {
                Alert.Show("Digite o CPF do professor.");
                return false;
            }

            if (!Validator.ValidateCPF(txtCPF.Text))
            {
                Alert.Show("Digite um CPF válido");
                return false;
            }
            return true;
        }
        private void LoadProfessorSubject()
        {
            UsefullStuffs.LoadProfessorSubjectBySubjectId(lstAddedSubjects, professorId,true);
            UsefullStuffs.LoadSubjects(lstSubjects);
            foreach (ListItem listItem in lstAddedSubjects.Items)
            {
                lstSubjects.Items.Remove(lstSubjects.Items.FindByValue(listItem.Value));
            }
        }
        #endregion




    }
}